package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.queryObject;
import cn.wolfcode.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        if(ids!=null&&ids.length>0){
            //处理中间表
            for(Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(),roleId);
            }
        }

    }

    @Override
        @Transactional(readOnly = true)
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);

    }

    @Override
    @Transactional(readOnly = true)
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
     employeeMapper.updateByPrimaryKey(employee);

     //删除原本的关系
     employeeMapper.deleteRelation(employee.getId());

        if(ids!=null&&ids.length>0){
            //处理中间表
            for(Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(),roleId);
            }
        }
    }





    @Override
    public PageResult query(queryObject qo) {
        int totalCount = employeeMapper.selectForCount(qo);
        if(totalCount==0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.emptyList());
        }

        List<Employee> listData = employeeMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),totalCount,listData);
    }

    @Override
    public  Employee login(String username, String password) {
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if(employee == null)  {     //登录失败
           throw new RuntimeException("账号密码不匹配");
    }
          return employee;
    }

    @Override
    public void updatePwd(String newPassword, String oldPassword,Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);

        if(!(employee.getPassword().equals(oldPassword))){
             throw new RuntimeException("原密码与旧密码不匹配");
         }
           employeeMapper.updatePwdById(newPassword,id);
    }

    @Override
    public void resetPwdById(String newPassword, Long id) {
        if(StringUtils.hasLength(newPassword)){
            throw new RuntimeException("修改密码不能为空");
        }
        employeeMapper.resetPwdById(newPassword,id);
    }

}
